package zisu.netty.practice.netty.inBoundAndOutBound;



import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ReplayingDecoder;
import zisu.netty.practice.netty.secs.entity.HSMSMessage;

import java.util.List;

/**
 * @Desc: ""
 * @Author: caixiang
 * @DATE: 2020/8/3 14:43
 */
public class MyByteToLongDecoder2 extends ReplayingDecoder<Void> {
    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
        System.out.println("MyByteToLongDecoder2 被调用");
        //在ReplayingDecoder 中不需要再判断数据是否足够读取,它内部会进行处理判断。
        //0 0 0 10 -1 -1 0 0 0 1 0 0 0 1
        if(in.readableBytes() >= 4){
            HSMSMessage hsmsMessage = new HSMSMessage();
            ByteBuf buf = in.readBytes(4);
            byte[] lenBytes = new byte[4];
            buf.readBytes(lenBytes);

            ByteBuf headerBuffer = in.readBytes(10);
            byte[] headerBytes = new byte[10];
            headerBuffer.readBytes(headerBytes);
            hsmsMessage.setLength(lenBytes);
            hsmsMessage.setHeader(headerBytes);
            out.add(hsmsMessage);
        }
    }
}
